package com.example.qu_tao.View;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.qu_tao.R;

public class WodeFragment extends Fragment implements View.OnClickListener {
    private LinearLayout setting;
    private LinearLayout dingdan;
    private LinearLayout daifukuan;
    private LinearLayout daifahuo;
    private LinearLayout daishouhuo;
    private LinearLayout daipingjia;
    private LinearLayout shouhou;
    private TextView userNameTextView;
    private TextView userPhoneTextView;
    private ImageView userProfileImage;
    private LinearLayout shoucangLayout;
    private LinearLayout gouwucheLayout;
    private LinearLayout zujiLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.fargment_wode, container, false);
        initView(mView);
        initListener();
        return mView;
    }

    private void initView(View mView) {
        setting = mView.findViewById(R.id.setting);
        dingdan = mView.findViewById(R.id.dingdan);
        daifukuan = mView.findViewById(R.id.layout_daifukuan);
        daifahuo = mView.findViewById(R.id.layout_daifahuo);
        daishouhuo = mView.findViewById(R.id.layout_daishouhuo);
        daipingjia = mView.findViewById(R.id.layout_daipingjia);
        shouhou = mView.findViewById(R.id.layout_shouhou);
        userNameTextView = mView.findViewById(R.id.user_name);
        userPhoneTextView = mView.findViewById(R.id.user_phone);
        userProfileImage = mView.findViewById(R.id.user_profile_image);
        shoucangLayout = mView.findViewById(R.id.layout_shoucang);
        gouwucheLayout = mView.findViewById(R.id.layout_gouwuche);
        zujiLayout = mView.findViewById(R.id.layout_zuji);
    }

    private void initListener() {
        setting.setOnClickListener(this);
        dingdan.setOnClickListener(this);
        daifukuan.setOnClickListener(this);
        daifahuo.setOnClickListener(this);
        daishouhuo.setOnClickListener(this);
        daipingjia.setOnClickListener(this);
        shouhou.setOnClickListener(this);
        shoucangLayout.setOnClickListener(this);
        gouwucheLayout.setOnClickListener(this);
        zujiLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.setting) {
            Intent intent = new Intent(getActivity(), shezhiActivity.class);
            startActivity(intent);
        } else if (id == R.id.dingdan) {
            Intent intent = new Intent(getActivity(), DingDanActivity.class);
            startActivity(intent);
        } else if (id == R.id.layout_daifukuan) {
            Intent intent = new Intent(getActivity(), DingDanActivity.class);
            startActivity(intent);
        } else if (id == R.id.layout_daifahuo) {
            Intent intent = new Intent(getActivity(), DingDanActivity.class);
            startActivity(intent);
        } else if (id == R.id.layout_daishouhuo) {
            Intent intent = new Intent(getActivity(), DingDanActivity.class);
            startActivity(intent);
        } else if (id == R.id.layout_daipingjia) {
            Intent intent = new Intent(getActivity(), DingDanActivity.class);
            startActivity(intent);
        } else if (id == R.id.layout_shouhou) {
            Intent intent = new Intent(getActivity(), DingDanActivity.class);
            startActivity(intent);
        } else if (id == R.id.layout_shoucang) {
            Intent intent = new Intent(getActivity(), ShouCangActivity.class); // 跳转到收藏页面
            startActivity(intent);
        } else if (id == R.id.layout_gouwuche) {
            Intent intent = new Intent(getActivity(), GouWuCheActivity.class); // 跳转到购物车页面
            startActivity(intent);
        }else if (id == R.id.layout_zuji) {
            Intent intent = new Intent(getActivity(), ZuJiActivity.class); // 跳转到足迹页面
            startActivity(intent);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        loadUserInfo();
        loadProfileImage();
    }

    private void loadUserInfo() {
        SharedPreferences sharedPreferences = requireContext().getSharedPreferences("user_info", requireContext().MODE_PRIVATE);
        long userId = sharedPreferences.getLong("user_id", -1);

        if (userId != -1) {
            Uri uri = Uri.parse("content://com.example.qu_tao.provider/user/" + userId);
            String[] projection = {"user_name", "user_phone"};
            Cursor cursor = requireContext().getContentResolver().query(uri, projection, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                @SuppressLint("Range") String userName = cursor.getString(cursor.getColumnIndex("user_name"));
                @SuppressLint("Range") String userPhone = cursor.getString(cursor.getColumnIndex("user_phone"));
                userNameTextView.setText(userName);
                userPhoneTextView.setText(userPhone);
                cursor.close();
            }
        }
    }

    private void loadProfileImage() {
        SharedPreferences sharedPreferences = requireContext().getSharedPreferences("user_info", requireContext().MODE_PRIVATE);
        String profileImagePath = sharedPreferences.getString("profile_image_path", null);
        if (profileImagePath != null) {
            // 压缩图片
            Bitmap bitmap = decodeSampledBitmapFromResource(profileImagePath, userProfileImage.getWidth(), userProfileImage.getHeight());
            if (bitmap != null) {
                userProfileImage.setImageBitmap(bitmap);
            }
        } else {
            // 显示默认头像
            userProfileImage.setImageResource(R.mipmap.haimianbaobao);
        }
    }

    private Bitmap decodeSampledBitmapFromResource(String path, int reqWidth, int reqHeight) {
        // 设置默认值避免除以零
        if (reqWidth <= 0) reqWidth = 300;
        if (reqHeight <= 0) reqHeight = 300;

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);
    }

    private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
            while ((halfHeight / inSampleSize) >= reqHeight
                    && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }
}